<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Db;
use think\Controller;
use think\Config;
use think\Session;
use think\Cookie;
use Godok\Org\Adminauth;
use Qcloud\cos\Api;

class System extends Common
{
	//小程序版本管理
	public function version(){
		$ret = Db::name('version')
			 ->where('is_delete',1)
			 ->select();
		$this->assign('list',$ret);
		//更新版本信息
		$eid = Request::instance()->get('eid','','number_int');

		if($eid){
			$up = DB::name('version')->where('id',$eid)->find();
			if($up){
				$this->assign('up',$up);
				return $this->fetch('edit');
			}
			
		}
		if ( Request::instance()->isPost()) {
			//删除版本信息
			$did = input("did");
			if($did){
				$ddata['id'] = $did;
				$ddata['is_delete'] = 0;
				if(Db::name( 'version')->update($ddata)) {
					return ['code'=>0, 'msg'=>'删除成功！',''];
				}else{
    				return ['code'=>-1050, 'msg'=>'删除失败！',''];
    			}	
			}
			//更新版本信息
			$data2 = input('post.');
			$name = $data2['name'];
			$vision = $data2['vision'];
			$check = Db::name('version')->where(['name'=>$name,'is_delete'=>1])->where('id','<>',$data2['id'])->find();
			if($check){
				return ['code'=>-1050, 'msg'=>'版本名称不能重名',''];
			}else{
				if(Db::name( 'version')->update($data2)){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="version"; $uid=session('adminid'); $querystring = '更新小程序版本信息'.$did;
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>'更新成功！',''];
	    		}else{
	    			return ['code'=>-1050, 'msg'=>'更新失败！',''];
	    		}
			}
		}
		
		return $this->fetch();
	}
	//添加版本信息
	public function versionadd(){
		if ( Request::instance()->isPost()) {
			$data = input('post.');
			$data['create_user'] = session('adminid');
			$data['create_time'] = time();
			$data['is_delete'] = 1;
			$name = $data['name'];
			$vision = $data['vision'];
			$check = Db::name('version')->where(['name'=>$name,'is_delete'=>1])->find();
			if($check){
				return ['code'=>-1050, 'msg'=>'版本名称不能重名',''];
			}else{
				if(Db::name( 'version')->insert($data)){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="versionadd"; $uid=session('adminid'); $querystring = '添加 '.$name.' 小程序版本信息';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>'添加成功！',''];
	    		}else{
	    			return ['code'=>-1050, 'msg'=>'添加失败！',''];
	    		}
			}
		}
		return $this->fetch('add');
	}
	//获取小程序服务地址
	public function serviceaddr(){
		
		if ( Request::instance()->isPost()) {
			$did = input("did");
			if($did){
				$ddata['id'] = $did;
				$ddata['is_delete'] = 0;
				if(Db::name( 'smallapp_serviceaddr')->update($ddata)) {
					return ['code'=>0, 'msg'=>'删除成功！',''];
				}else{
    				return ['code'=>-1050, 'msg'=>'删除失败！',''];
    			}	
			}
			$data2 = input('post.');
			if(Db::name( 'smallapp_serviceaddr')->update($data2) !==false){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="serviceaddr"; $uid=session('adminid'); $querystring = '更新 '.$data2.' 小程序服务器地址';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>'更新成功！',''];
	    	}else{
	    		return ['code'=>-1050, 'msg'=>'更新失败！',''];
	    	}

		}else{
			$ret= Db::name('smallapp_serviceaddr')
			->where('is_delete',1)
			->select();
			$this->assign('list',$ret);
			$eid = Request::instance()->get('eid','','number_int');
			if($eid){
				$up = DB::name('smallapp_serviceaddr')->where('id',$eid)->find();
				if($up){
					$this->assign('up',$up);
					
					return $this->fetch('serviceedit');
				}
				
			}

			return $this->fetch();
		}
	}

	public function addservice(){
		if ( Request::instance()->isPost()) {
			$data = input('post.');
			$data['is_delete'] = 1;
			if(Db::name('smallapp_serviceaddr')->insert($data)){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="addservice"; $uid=session('adminid'); $querystring = '添加 '.input('addr').' 小程序服务器地址';
	                $ip = get_ip();$os = get_os();
	                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
				return ['code'=>0, 'msg'=>'添加成功！',''];
			}else{
				return ['code'=>-1050, 'msg'=>'更新失败！',''];
			}
		}else{
			return $this->fetch();
		}
	}

	public function theme(){

		
		if(input('id')){
			if(!empty(input('id'))){
				$update = Db::name('smallapp_theme')->where('id',input('id'))->find();
				$this->assign('v',$update);
				return $this->fetch('edittheme');
			}else{
				die('非法访问!');
			}
		}
		
		$ret =Db::name('smallapp_theme')->select();
			$this->assign('list',$ret);
			
			return $this->fetch();
	}

	public function addtheme(){
		if ( Request::instance()->isPost()) {
			$data = input('post.');
			$cosApi = new Api(config('Qcloud'));
			$bucket = config('Qcloud.bucket');
			//$ret = $cosApi->upload($bucket, $src, $dst);
			if($_FILES['index_img']['name']){
				$file = Request::instance()->file('index_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
				if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['index_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
		        
			}
			if($_FILES['user_img']['name']){
				$file = Request::instance()->file('user_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
				if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['user_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
		        //$data['user_img'] = '/uploads/'.$file;
			}
			if($_FILES['recharge_img']['name']){
				$file = Request::instance()->file('recharge_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
				if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['recharge_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
		        //$data['recharge_img'] = '/uploads/'.$file;
			}
			if(Db::name( 'smallapp_theme')->insert($data)){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="addtheme"; $uid=session('adminid'); $querystring = '添加 '.input('name').' 主题信息';
	                $ip = get_ip();$os = get_os();
	                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>'添加成功！'];
    		}else{
    			return ['code'=>-1050, 'msg'=>'添加失败！'];
    		}
		}
		return $this->fetch();
	}

	public function edittheme(){
		if ( Request::instance()->isPost()) {

			$data = input('post.');
			if($_FILES['index_img']['name']){
				$file = Request::instance()->file('index_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
		        if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['index_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
			}
			if($_FILES['user_img']['name']){
				$file = Request::instance()->file('user_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
		        if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['user_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
			}
			if($_FILES['recharge_img']['name']){
				$file = Request::instance()->file('recharge_img');
				$info=$file->move(ROOT_PATH . 'public' . DS . 'uploads');
				$file = $info->getSaveName();
		        if($file){
                    $cosApi = new Api(config('Qcloud'));
                    $bucket = config('Qcloud.bucket');
                    $src = ROOT_PATH . 'public/uploads/'.$file;
                    $dst = 'admin/theme/'. $file;
                    $ret = $cosApi->upload($bucket, $src, $dst);
					if($ret['code']==0){
	                    $path = $ret['data']['resource_path'];  //获取资源文件路径（包含了APPID和bucketname）
	                    $len = strlen($bucket);  //计算bucketname长度
	                    $start = strpos($path,$bucket);   //计算bucketname第一次出现的问题
	                    $start +=$len;  
	                    $newpath = substr($path,$start);  //截取路径
	                    unlink($src);  //删除服务器上面的资源文件
	                    $res['code'] = '1';
	                    $res['picture'] = $newpath;  //资源文件路径（需要前面添加上域名地址才可以使用）
	                    $res['url'] = $ret['data']['access_url'];   //资源文件URL
	                    $data['recharge_img'] = $res['picture'];
	                    //return $res;
	                }   
                }
			}
			if(Db::name( 'smallapp_theme')->update($data)){
				$oid = 0;$m = 'ausadmin'; $c = 'System'; $action="edittheme"; $uid=session('adminid'); $querystring = '编辑 '.input('name').' 主题信息';
	                $ip = get_ip();$os = get_os();
	                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			return ['code'=>0, 'msg'=>'添加成功！'];
    		}else{
    			return ['code'=>-1050, 'msg'=>'添加失败！'];
    		}
		}
	}

 
	
}